package com.fmdj.cst.service.impl;
import cn.hutool.core.map.MapUtil;
import com.fmdj.common.exception.GlobalException;
import com.fmdj.common.util.MicroAppUtil;
import com.fmdj.cst.db.dao.CustomerDao;
import com.fmdj.cst.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@Service
public class CustomerServiceImpl implements CustomerService {
    @Autowired
    private CustomerDao customerDao;
    @Resource
    private MicroAppUtil microAppUtil;
    @Override
    public String registerNewCustomer(Map param) {
        String code = MapUtil.getStr(param, "code");
        String openId = microAppUtil.getOpenId(code);
        HashMap tempParam = new HashMap() {{
            put("openId", openId);
        }};

        if (customerDao.hasCustomer(tempParam) != 0) {
            throw new GlobalException("该微信已经注册过");
        }

        //注册用户
        param.put("openId", openId);
        customerDao.registerNewCustomer(param);
        //返回用户id
        return customerDao.selectCustomerIdByOpenId(openId);
    }

    @Override
    public String login(String openId) {
        return customerDao.login(openId);
    }

    @Override
    public HashMap selectCustomerInfoInOrderForm(Long customerId) {
        return customerDao.selectCustomerInfoInOrderForm(customerId);
    }

    @Override
    public HashMap searchCustomerBriefInfo(long customerId) {
        HashMap hashMap = customerDao.searchCustomerBriefInfo(customerId);
        return hashMap;
    }

    @Override
    public String searchCustomerOpenId(long customerId) {
        return customerDao.searchCustomerOpenId(customerId);
    }
}
